﻿using DataScripter.Database;
using DataScripter.Scripting.Filters;
using DataScripter.Scripting.ScriptDefinitions;

namespace DataScripter.Scripting.Fluent
{
    public class FluentOrFilterRequest : IFluentOrFilterRequest
    {
        public IScriptDefinition ScriptDefinition { get; private set; }

        private readonly OrFilter _orFilter;

        private ITableScriptRequest TableScriptRequest { get; set; }
        private IFluentTableScriptRequest FluentTableScriptRequest { get; set; }

        public FluentOrFilterRequest(IFluentTableScriptRequest fluentTableScriptRequest)
        {
            FluentTableScriptRequest = fluentTableScriptRequest;
            ScriptDefinition = FluentTableScriptRequest.ScriptDefinition;
            _orFilter = new OrFilter();
            TableScriptRequest = FluentTableScriptRequest.Request;
            TableScriptRequest.Filters.Add("OR", _orFilter);
        }

        public IFluentTableScriptRequest EndOr()
        {
            return FluentTableScriptRequest;
        }

        public IFluentOrFilterRequest FilterByForeignKey(IForeignKey foreignKey)
        {
            _orFilter.Add(new ForeignKeyFilter(foreignKey, TableScriptRequest, ScriptDefinition));
            return this;
        }

        public IFluentOrFilterRequest Where(string whereClause)
        {
            _orFilter.Add(new WhereFilter(whereClause));
            return this;
        }

        public string TableName
        {
            get { return TableScriptRequest.TableName; }
        }

        public ITableScriptRequest Request
        {
            get { return FluentTableScriptRequest.Request; }
        }
    }
}